{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6931d00d-1b3f-4630-820f-7537ce8a243a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入总头数：  3\n",
      "请输入总脚数(必须是偶数)： 2\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "方法一：无解，请重新运行测试！\n",
      "方法二：无解，请重新运行测试！\n"
     ]
    }
   ],
   "source": [
    "h = int(input(\"请输入总头数： \"))       #total heads of chicken & rabbit\n",
    "f = 1\n",
    "while (f % 2 != 0):\n",
    "    f = int(input(\"请输入总脚数(必须是偶数)：\"))        #total feet of chicken & rabbit\n",
    "# 方法一：利用循环\n",
    "solution = False     # 判断是否有解\n",
    "for c in range(0,h+1):\n",
    "    r = h - c\n",
    "    if (2 * c + 4 * r == f):\n",
    "        print(str.format(\"方法一：鸡：{0:1.0f} 只， 兔：{1:1.0f} 只\", c, r))\n",
    "        solution = True\n",
    "if (not solution): print(\"方法一：无解，请重新运行测试！\")\n",
    "#方法二：解方程\n",
    "r = f / 2 - h\n",
    "c = h - r\n",
    "solution = False     # 判断是否有解\n",
    "if (r >= 0 and  c >= 0):\n",
    "    #print(str.format(\"方法二：鸡：{0:1.0f} 只， 兔：{1:1.0f} 只\", c, r))\n",
    "    print(str.format(\"{0:1.0f} {1:1.0f}\", c, r))\n",
    "    solution = True\n",
    "if (not solution): print(\"方法二：无解，请重新运行测试！\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2ae87323-20d5-453b-9f1a-a5ece08e36e4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
